package com.bytech.common.biz.intercept;

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.bytech.common.annotation.UserAgent;
import com.bytech.common.biz.entity.blogUser.BlogUserAgent;
import com.bytech.common.config.AppGlobalConstants;

/**
 * controller 入参处理
 * @Author zhao
 * @Create 2021/7/2817:02
 */
@Component
public class BlogUserAgentMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(UserAgent.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
        BlogUserAgent userAgent = (BlogUserAgent) webRequest.getAttribute(AppGlobalConstants.BLOG_USER_SESSION_KEY, RequestAttributes.SCOPE_SESSION);
        return userAgent == null ? new BlogUserAgent() : userAgent;
    }
}
